package com.leyou.auth.controller;

import com.leyou.auth.service.AuthService;
import com.leyou.common.auth.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.auth.controller
 * @date 2019/7/7 19:17
 * @description:
 **/
@RestController
public class AuthController {

    @Autowired
   private AuthService authService;

    /**
     * 登录
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Void> login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpServletResponse response) {
        authService.login(username,password,response);
       return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 验证用户信息
     * @param request
     * @return
     */
    @GetMapping("verify")
    public ResponseEntity<UserInfo> verityUser(HttpServletRequest request,HttpServletResponse response){
        return ResponseEntity.ok(authService.verityUser(request,response));
    }


    /**
     * 注销登录和退出
     * @param request
     * @param response
     * @return
     */
    @PostMapping("logout")
    public ResponseEntity<Void> loginOut(HttpServletRequest request,HttpServletResponse response){
        authService.loginOut(request,response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 授权中心给自己授权生成token
     * @param id
     * @param secret
     * @return
     */
    @GetMapping("authorization")
    public ResponseEntity<String> authorization(
            @RequestParam("id") Long id,@RequestParam("secret") String secret){
        return ResponseEntity.ok(authService.authorization(id,secret));
    }


}
